<template>
  <div class="top">
    <div class="name">{{ nodeData.name }}</div>
    <div class="manager">{{ nodeData.manager }}</div>
    <div class="option">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link">
          操作<i class="el-icon-arrow-down el-icon--right"></i>
        </span>
        <el-dropdown-menu slot="dropdown">
          <el-dropdown-item command="add">新增</el-dropdown-item>
          <el-dropdown-item command="edit" v-if="isShow">修改</el-dropdown-item>
          <el-dropdown-item command="remove" v-if="isShow"
            >删除</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
    </div>
  </div>
</template>

<script>
// 删除部门
import { deleteDepartment } from '@/api/departments'
export default {
  props: {
    nodeData: {
      type: Object,
      required: true
    },
    isShow: {
      type: Boolean
    }
  },
  data () {
    return {}
  },
  methods: {
    // 下拉菜单的点击事件
    handleCommand (command) {
      switch (command) {
        case 'add':
          this.$bus.$emit('isShowEvent', this.nodeData, 'add')
          break
        case 'edit':
          this.$bus.$emit('edit', this.nodeData, 'edit')
          break
        case 'remove':
          // 弹出提示框 是否确认要删除
          this.$confirm('将要永久删除，确定继续吗', '温馨提示').then(
            async () => {
              // 发送请求
              const res = await deleteDepartment(this.nodeData.id)
              // console.log(res)
              this.$message.success('删除成功')
              this.$emit('getTreeData')
            }
          )
          break
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.top {
  width: 100%;
  display: flex;
  .name {
    flex: 1;
  }
  .manager {
    margin-right: 50px;
  }
}
</style>
